package zqkcom.atguigu.servlet;

import zqkcom.atguigu.service.UserService;
import zqkcom.atguigu.service.impl.UserServiceImpl;
import zqkcom.atguigu.bean.User;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserServlet extends BaseServlet {

    /**
     * 实现登入功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //显示业务逻辑的处理，即调用service
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);
        //判断通过用户名和密码查询的用户对象是否为null
        if(user == null){
            //将错误提示信息在请求域中进行共享
            request.setAttribute("errorMsg","密码或账户错误哦，调整心态再试试吧");
            //通过用户名和密码查询的用户对象为null，即登录失败，需要使用转发跳转到登录页面
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }else{
            //将登录成功的用户信息在session中进行共享
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            //通过用户名和密码查询的用户对象不为null，即登录成功，需要使用重定向跳转到登录成功页面
            response.sendRedirect(request.getContextPath() + "/pages/user/login_success.jsp");
        }
    }

    /**
     * 实现注册功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        //先判断验证码是否正确，正确的验证码在session中，对应的属性名为sessionCode
        HttpSession session = request.getSession();
        String sessionCode = (String) session.getAttribute("sessionCode");
        if (code.equals(sessionCode)) {
            //处理业务逻辑，即调用service
            UserService userService = new UserServiceImpl();
            //将从浏览器端收集的用户信息封装到User对象中
            User user = new User(null, username, password, email);
            boolean result = userService.regist(user);
            //判断注册的结果
            if (result) {
                //表示注册成功，需要使用重定向跳转到注册成功页面
                response.sendRedirect(request.getContextPath() + "/pages/user/regist_success.jsp");
            } else {
                request.setAttribute("errorMsg", "用户名已经被注册");
                request.setAttribute("username", username);
                request.setAttribute("email", email);

                //表示注册失败，需要通过转发跳转到原页面即注册页面
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            }
        } else {
            //不正确的话--》请求域 + 转发
            //将错误信息在域共享
            request.setAttribute("errorMsg", "验证码错误哦宝贝");
            //表示注册失败，需要通过转发跳转到原页面即注册页面
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }

    }

    /**
     * 实现注册功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void registByAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        //先判断验证码是否正确，正确的验证码在session中，对应的属性名为sessionCode
        HttpSession session = request.getSession();
        String sessionCode = (String) session.getAttribute("sessionCode");
        if (code.equals(sessionCode)) {
            //处理业务逻辑，即调用service
            UserService userService = new UserServiceImpl();
            //将从浏览器端收集的用户信息封装到User对象中
            User user = new User(null, username, password, email);
            boolean result = userService.regist(user);
            //判断注册的结果
            if (result) {
                //表示注册成功，需要使用重定向跳转到注册成功页面
                response.getWriter().print("success");
            } else {
                response.getWriter().print("usernameError");
            }
        } else {
            response.getWriter().print("codeError");
        }

    }


    /**
     * 注销也叫（登出，退出）功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    protected void logout (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session对象
        HttpSession session = request.getSession();
        //将session中共享的信息删除
        //session.removeAttribute("user");
//        session.invalidate();//失效，让session立即失效
        session.invalidate();//使session 立即失效
        //重定向到主页面
        response.sendRedirect(request.getContextPath());//默认上下文就是主页

    }

}
